package com.rizzo.web.util.locale;

import com.rizzo.back.exception.ApplicationException;
import com.rizzo.web.delegator.UtilDelegator;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author Jan Lievens
 */
public class RizzoSessionLocaleResolver implements LocaleResolver {

    /**
     * Field utilDelegator
     */
    private UtilDelegator utilDelegator;

    /**
     * Name of the session attribute that holds the locale.
     * Only used internally by this implementation.
     * Use <code>RequestContext(Utils).getLocale()</code>
     * to retrieve the current locale in controllers or views.
     *
     * @see org.springframework.web.servlet.support.RequestContext#getLocale
     * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
     */
    public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";


    /**
     * Method resolveLocale ...
     *
     * @param request of type HttpServletRequest
     * @return Locale
     */
    public Locale resolveLocale(HttpServletRequest request) {
        Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
        if (locale == null) {
            try {
                locale = determineDefaultLocale(request);
            } catch (ApplicationException e) {
                // ultimate fallback setting
                locale = new Locale("en");
            }
        }
        return locale;
    }

    /**
     * Determine the default locale for the given request,
     * Called if no locale session attribute has been found.
     * <p>The default implementation returns the specified default locale,
     * if any, else falls back to the request's accept-header locale.
     *
     * @param request the request to resolve the locale for
     * @return the default locale (never <code>null</code>)
     * @see javax.servlet.http.HttpServletRequest#getLocale()
     */
    protected Locale determineDefaultLocale(HttpServletRequest request) throws ApplicationException {
        Locale defaultLocale = this.utilDelegator.getDefaultLocale();
        if (defaultLocale == null) {
            defaultLocale = request.getLocale();
        }
        return defaultLocale;
    }

    /**
     * Method setLocale ...
     *
     * @param request  of type HttpServletRequest
     * @param response of type HttpServletResponse
     * @param locale   of type Locale
     */
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
    }

    /**
     * Method setUtilDelegator sets the utilDelegator of this GouseSessionLocaleResolver object.
     *
     * @param utilDelegator the utilDelegator of this GouseSessionLocaleResolver object.
     */
    public void setUtilDelegator(UtilDelegator utilDelegator) {
        this.utilDelegator = utilDelegator;
    }
}
